package com.miguo.bean;

import com.miguo.framework.request.JsonData;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class CommentBean implements Serializable {

    private static final long serialVersionUID = 1L;

    public static List<GoodsBean> GoodsData(JsonData items) {
        List<GoodsBean> mGoodsList = new ArrayList<GoodsBean>();
        if (items.length() > 0) {
            for (int i = 0; i < items.length(); i++) {
                JsonData GoodsObject = items.optJson(i);
                GoodsBean Data = new GoodsBean(GoodsObject);
                mGoodsList.add(Data);
            }
        }
        return mGoodsList;
    }

    public static List<SearchKeyBean> SearchKey(JsonData data) {
        List<SearchKeyBean> mSearchKeyList = new ArrayList<SearchKeyBean>();
        for (int i = 0; i < data.length(); i++) {
            JsonData SearchKeyObject = data.optJson(i);
            SearchKeyBean Data = new SearchKeyBean(SearchKeyObject);
            mSearchKeyList.add(Data);
        }
        return mSearchKeyList;
    }

    public static List<CategoryBean> Category(JsonData category) {
        List<CategoryBean> mCategoryList = new ArrayList<CategoryBean>();
        for (int i = 0; i < category.length(); i++) {
            JsonData CategoryObject = category.optJson(i);
            CategoryBean Data = new CategoryBean(CategoryObject);
            mCategoryList.add(Data);
        }
        return mCategoryList;
    }

    public static List<CategoryThemeBean> CategoryTheme(JsonData theme) {
        List<CategoryThemeBean> mThemeList = new ArrayList<CategoryThemeBean>();
        for (int i = 0; i < theme.length(); i++) {
            JsonData ThemeObject = theme.optJson(i);
            CategoryThemeBean Data = new CategoryThemeBean(ThemeObject);
            mThemeList.add(Data);
        }
        return mThemeList;
    }

    public static List<CategoryBrandBean> CategoryBrand(JsonData brand) {
        List<CategoryBrandBean> mBrandList = new ArrayList<CategoryBrandBean>();
        for (int i = 0; i < brand.length(); i++) {
            JsonData BrandObject = brand.optJson(i);
            CategoryBrandBean Data = new CategoryBrandBean(BrandObject);
            mBrandList.add(Data);
        }
        return mBrandList;
    }

    public static List<GoodsAdvertBean> GoodsAdvertData(JsonData json) {
        List<GoodsAdvertBean> mGoodsAdvertList = new ArrayList<GoodsAdvertBean>();

        if (null != json) {
            int AdvertCount = json.optInt("ad_count");
            JsonData page = json.optJson("page");
            int ItemCount = page.optInt("count");
            if (AdvertCount > 0) {
                JsonData Advert = json.optJson("ad");
                for (int i = 0; i < Advert.length(); i++) {
                    JsonData AdvertObject = Advert.optJson(i);
                    AdvertBean AdvertData = new AdvertBean(AdvertObject);
                    GoodsAdvertBean GoodsAdData = new GoodsAdvertBean();
                    GoodsAdData.setAdvert(AdvertData);
                    GoodsAdData.setIsGoods(false);
                    mGoodsAdvertList.add(GoodsAdData);
                }
            }

            if (ItemCount > 0) {
                JsonData Item = json.optJson("item");
                for (int i = 0; i < Item.length(); i++) {
                    JsonData GoodsObject = Item.optJson(i);
                    GoodsBean GoodsData = new GoodsBean(GoodsObject);
                    GoodsAdvertBean GoodsAdData = new GoodsAdvertBean();
                    GoodsAdData.setGoods(GoodsData);
                    GoodsAdData.setIsGoods(true);
                    mGoodsAdvertList.add(GoodsAdData);
                }
            }

        }
        return mGoodsAdvertList;
    }

    public static List<AdvertBean> AdvertData(JsonData data) {
        List<AdvertBean> mAdvertList = new ArrayList<AdvertBean>();
        for (int i = 0; i < data.length(); i++) {
            JsonData AdvertObject = data.optJson(i);
            AdvertBean Data = new AdvertBean(AdvertObject);
            mAdvertList.add(Data);
        }
        return mAdvertList;
    }

    public static List<AddressBean> AddressData(String json) {
        List<AddressBean> mAddressList = new ArrayList<AddressBean>();
        try {
            JSONArray Array = new JSONArray(json);
            for (int i = 0; i < Array.length(); i++) {
                JSONObject AdvertObject = Array.getJSONObject(i);
                AddressBean Data = new AddressBean(AdvertObject);
                mAddressList.add(Data);
            }
            return mAddressList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mAddressList;
    }

    public static List<ConfigBean> ConfigData(JsonData data) {
        List<ConfigBean> mConfigList = new ArrayList<ConfigBean>();
        Iterator it = data.keys();
        while (it.hasNext()) {
            String name = (String) it.next();
            String value = data.optString(name);
            ConfigBean Data = new ConfigBean(name, value);
            mConfigList.add(Data);
        }
        return mConfigList;

    }

}
